/**
	Pulsar engine. Core.
	File manager implementation - windows extension

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

namespace pl
{

class FileManagerImplWin;

class FileManagerImplWin : public FileManagerImpl
{
public:
	FileManagerImplWin();
	virtual ~FileManagerImplWin();

	bool createDrives();

	static bool convertWin2Fs(DWORD attribWin, pl_uint32& attribFs);
	static bool convertFs2Win(pl_uint32 attribFs, DWORD& attribWin);

	static void _convertParams(pl_uint32 options, DWORD& share, DWORD& openAttrib);

	virtual pl::FileError copyFile(const pl::Filepath& oldName, const pl::Filepath& newName, bool bForce);

	virtual bool convertToFullPath(pl::Filepath& fileName);
};

class StreamReadWin : public StreamRead
{
public:
	StreamReadWin(const pl::Filepath fileName, pl_uint32 options);
	~StreamReadWin();

	virtual bool isInited() const
	{
		return (_hFile == INVALID_HANDLE_VALUE) ? false : true;
	}

	virtual pl_int64 getFileSize() const;

	virtual pl_int64 tell() const;

	virtual pl_int64 seek(pl_int64 pos, SeekInfo where);

	virtual bool isEOF() const;

	virtual bool readBuffer(void* pBuf, pl_int32 itemSize, pl_int32 itemCount, pl_int64* lengthRead = NULL);

	virtual bool readString(pl_char* pStr, int maxLen, pl_char termChar = PL_T('\0'));

private:
	HANDLE _hFile;
};

class StreamWriteWin : public StreamWrite
{
public:
	StreamWriteWin(const pl::Filepath fileName, pl_uint32 options);
	~StreamWriteWin();

	virtual bool isInited() const
	{
		return (_hFile == INVALID_HANDLE_VALUE) ? false : true;
	}

	virtual void disableSave() {}

	virtual int getMaxAlign() const
	{
		return _maxAlign;
	}

	virtual pl_int64 getFileSize() const;

	virtual pl_int64 tell() const;

	virtual pl_int64 seek(pl_int64 pos, SeekInfo where);

	virtual bool flush();

	virtual bool writeBuffer(const void* pBuf, pl_int32 itemSize, pl_int32 itemCount);

	virtual bool writeString(const pl_char* pStr, pl_char termChar = '\0');

private:
	HANDLE _hFile;
	int _maxAlign;
};

} // namespace pl

//
// End of file 'pulsar_filemanager_impl_win.h'
//
